
$(document).ready(function(e) {
    initTable();
});
/**
 * 初始化会员名录页面的会员表格
 */
function initTable() {
    var mTr = $("#mTr");

    var memberList = eval("("+$('#memberList').val()+")");
    var mlength = memberList.length;

    for(var i=0;i< mlength;i++){
        var index = mlength-1-i ;
        var num = mlength-i;
        var trId = "mtrId" + index;
        /*var tr = '<tr id="'+trId+'">' +
                    '<td>'+ num +'</td>' +
                    '<td>'+ memberList[index].name +'</td>' +
                    '<td>'+ memberList[index].unit +'</td>' +
                    '<td>'+ memberList[index].position +'</td>' +
                 '</tr>';*/
        var tr = '<tr id="'+trId+'">' +
            '<td>'+ num +'</td>' +
            '<td>'+ memberList[index].name +'</td>';
        if (memberList[index].link != null && memberList[index].link != ''){
            tr = tr + '<td><a href="'+ memberList[index].link +'" target="_blank" >'+ memberList[index].unit +'</a></td>';
        }else{
            tr = tr + '<td>'+ memberList[index].unit +'</td>';
        }
        tr = tr + '<td>'+ memberList[index].position +'</td></tr>';

        $(tr).insertAfter(mTr);
    }
}

/**
 * "点击查看更多"
 */
function loadMore(){

    var mTr = $("#mTr");

    // 后台的请求数据url
    var loadMoreUrl = "/member/getMoreMembers";

    // 清空表格
    removeTr();

    $.ajax({
        url:loadMoreUrl,
        type:"get",
        dataType:"json",
        success:function(data){
            var memberlList = eval(data);
            var mlength = memberlList.length;

            for(var i=0;i< mlength;i++){
                var index = mlength-1-i;
                var num = mlength-i;
                var trId = "mtrId" + index;
                /*var tr = '<tr id="'+trId+'">' +
                            '<td>'+ num +'</td>' +
                            '<td>'+ memberlList[index].name +'</td>' +
                            '<td>'+ memberlList[index].unit +'</td>' +
                            '<td>'+ memberlList[index].position +'</td>' +
                         '</tr>';*/
                var tr = '<tr id="'+trId+'">' +
                    '<td>'+ num +'</td>' +
                    '<td>'+ memberlList[index].name +'</td>';
                if (memberlList[index].link != null && memberlList[index].link != ''){
                    tr = tr + '<td><a href="'+ memberlList[index].link +'" target="_blank" >'+ memberlList[index].unit +'</a></td>';
                }else{
                    tr = tr + '<td>'+ memberlList[index].unit +'</td>';
                }
                tr = tr + '<td>'+ memberlList[index].position +'</td></tr>';
                $(tr).insertAfter(mTr);
            }
            $("#more").attr('style','display: none');
        },
        error:function(XMLHttpRequest,textStatus, errorThrown) {
            // alert(errorThrown);
        }
    });
}

// “点击查看更多”前先清除表格数据
function removeTr() {
    var mtable = document.getElementById("mtable") ;
    //表格行数(表头两行和“点击查看更多”一行)
    var mrows = mtable.rows.length;

    for (var i=0;i<mrows-3;i++){
        var trId = "mtrId" + i;
        $('#'+trId).remove();
    }

}